package com.jun.admin.core.controller;

import com.google.common.collect.Maps;
import com.jun.admin.core.model.User;
import com.jun.admin.core.model.aspect.SelectLog;
import com.jun.admin.core.model.constant.RedisKeyConstant;
import com.jun.admin.core.service.IndexService;
import com.jun.common.core.entity.page.ResultHelper;
import com.jun.common.redis.pubsub.RedisPublisher;
import com.jun.common.redis.stream.RedisStreamPublisher;
import com.jun.sso.core.conf.Conf;
import com.jun.sso.core.user.XxlSsoUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;

/**
 * @author yuejun.li
 * @date 2024/4/28 17:54:47
 */
@Api(tags = "练习controller")
@RestController
@RequestMapping("index")
@AllArgsConstructor
@Slf4j
public class IndexController {

    private IndexService indexService;
    private RedisPublisher publisher;
    private RedisTemplate redisTemplate;
    private RedisStreamPublisher redisStreamPublisher;

    @GetMapping("json")
    @SelectLog
    public ResultHelper<XxlSsoUser> json(HttpServletRequest request) {
        XxlSsoUser xxlUser = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
        return ResultHelper.success(xxlUser);
    }

    @GetMapping("test")
    @ApiOperation("测试事务传播")
    public ResultHelper<Object> transactional(){
        boolean flag = indexService.transactional();
        if(!flag){
            return ResultHelper.fail();
        }
        return ResultHelper.success();
    }

    @ApiOperation("测试策略")
    @GetMapping("strategy")
    public ResultHelper<Object> strategy(){
        return ResultHelper.success(indexService.strategy());
    }

    @ApiOperation("测试多线程")
    @GetMapping("thread")
    public ResultHelper<Boolean> thread() throws Throwable {
        return ResultHelper.success(indexService.thread());
    }

    @ApiOperation("测试脱敏")
    @GetMapping("desensitization")
    public ResultHelper<Object> desensitization(){
        return ResultHelper.success(indexService.desensitization());
    }


    @ApiOperation("测试延迟双删")
    @GetMapping("update")
    public ResultHelper<Object> update(String id){
        return ResultHelper.success(indexService.update(id));
    }

    @ApiOperation("测试分布式锁")
    @GetMapping("lock")
    public ResultHelper<Boolean> lock(String id,Integer age) throws InterruptedException {
        return ResultHelper.success(indexService.lock(id,age));
    }

    @ApiOperation("测试分布式锁")
    @PostMapping("lockBean")
    public ResultHelper<Boolean> lockBean(@RequestBody User user) throws InterruptedException {
        return ResultHelper.success(indexService.lockBean(user));
    }

    @ApiOperation("测试redis主从")
    @GetMapping("redis")
    public void redis() throws InterruptedException {
        indexService.redis();
    }

    @ApiOperation("测试redis的发布订阅")
    @GetMapping("publish")
    public void publish()  {
        HashMap<String, String> hashMap = Maps.newHashMap();
        hashMap.put("name","李岳俊");
        hashMap.put("id",System.currentTimeMillis()+"");
        redisStreamPublisher.produce(RedisKeyConstant.REDIS_STREAM_QUEUE,hashMap);
    }

    @ApiOperation("测试异步编排")
    @GetMapping("completableFuture")
    public void completableFuture() throws ExecutionException, InterruptedException {
        indexService.completableFuture();
    }

    @ApiOperation("测试redis的消息队列-基于list")
    @GetMapping("listmq")
    public void listMQ()  {
        String str = "你好".concat(System.currentTimeMillis() + "") ;
        log.info("推送消息：{}",str);
        redisTemplate.opsForZSet().add(RedisKeyConstant.REDIS_ZSET_QUEUE_KEY,str,System.currentTimeMillis());
    }

}


